package com.web.controller;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.web.model.PageEdit;
import com.web.model.PageFirstType;
import com.web.model.PageInfo;
import com.web.model.PageSecondType;
import com.web.util.GetEditPageModel;
import com.web.util.Properties;
import org.apache.log4j.Logger;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

/**
 * Created by reeves on 2015/7/5.
 */
public class PageController extends Controller {
    /*创建log4j对象*/
    private static final Logger LOGGER = Logger.getLogger(UserController.class);

    /**
     * 转到推荐集中营界面
     * 调用接口：http://ipaderess/editorJfinalProject/page/gotoRecomPage/pageNumber
     */
    public void gotoRecomPage() {
        int pageNumber = getParaToInt(1);

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        Page<Record> pageRecom = PageEdit.pageEditDao.getRecom(pageNumber, Properties.PageSize_Main);
        pageList = pageRecom.getList();

        if (pageRecom.getList().isEmpty()) {
            LOGGER.error("查询推荐信息出错，未查找到推荐信息");
        }

        totalPage = PageEdit.pageEditDao.getRecomNumber(Properties.PageSize_Main);

        /*分页信息发送到页面*/
        setAttr("pageInfoList", pageList);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("recomCollect.jsp"); // 推荐信息集中营页面：recomCollect.jsp

    }

    /**
     * 推荐集中营获取一级分类入口
     */
    public void gotoRecomFirst(){
        int pageNumber = getParaToInt(1);
        int firstType = getParaToInt(0);

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        Page<Record> pageRecom = PageEdit.pageEditDao.getRecomFirst(pageNumber, Properties.PageSize_Main, firstType);
        pageList = pageRecom.getList();

        if (pageRecom.getList().isEmpty()) {
            LOGGER.error("查询推荐信息出错，未查找到推荐信息");
        }

        totalPage = PageEdit.pageEditDao.getRecomNumber(Properties.PageSize_Main);

        /*分页信息发送到页面*/
        setAttr("pageInfoList", pageList);
        setAttr("firstType", firstType);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("recomFirstCollect.jsp"); // 推荐信息集中营页面：recomCollect.jsp
    }

    /**
     * 推荐集中营获取二级分类入口
     */
    public void gotoRecomSecond(){
        int pageNumber = getParaToInt(1);
        int second = getParaToInt(0);

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        Page<Record> pageRecom = PageEdit.pageEditDao.getRecomSecond(pageNumber, Properties.PageSize_Main, second);
        pageList = pageRecom.getList();

        if (pageRecom.getList().isEmpty()) {
            LOGGER.error("查询推荐信息出错，未查找到推荐信息");
        }

        totalPage = PageEdit.pageEditDao.getRecomNumber(Properties.PageSize_Main);

        /*分页信息发送到页面*/
        setAttr("pageInfoList", pageList);
        setAttr("secondType", second);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("recomSecondCollect.jsp"); // 推荐信息集中营页面：recomCollect.jsp

    }

    /**
     * 推荐集中营三级分类入口
     */
    public void gotoRecomThird(){
        int pageNumber = getParaToInt(1);
        int third = getParaToInt(0);

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        Page<Record> pageRecom = PageEdit.pageEditDao.getRecomThird(pageNumber, Properties.PageSize_Main, third);
        pageList = pageRecom.getList();

        if (pageRecom.getList().isEmpty()) {
            LOGGER.error("查询推荐信息出错，未查找到推荐信息");
        }

        totalPage = PageEdit.pageEditDao.getRecomNumber(Properties.PageSize_Main);

        /*分页信息发送到页面*/
        setAttr("pageInfoList", pageList);
        setAttr("thirdType", third);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("recomThirdCollect.jsp"); // 推荐信息集中营页面：recomCollect.jsp
    }

    /**
     * 按标题关键字搜索功能入口
     */
    public void gotoSearchPage(){
        String keyWord = getPara("keyWord");
        keyWord = "%"+keyWord+"%";
        List<Record> pageSearch = PageEdit.pageEditDao.getPageByKey(keyWord);
        if(pageSearch.isEmpty()){
            LOGGER.error("按关键字搜索失败！无此内容");
            renderHtml("Sorry,没有找到这篇文章。<br/>");
        }
        else{
//            System.out.println(pageSearch);
            setAttr("pageInfoList",pageSearch);
            renderJsp("infoPage.jsp");
        }

    }

    /**
     * 根据标题获取推送相关信息
     */
    public void getTuisong() {
        String keyWord = getPara("keyWord");
        keyWord = "%"+keyWord+"%";
        List<Record> tuiList = PageInfo.pageInfoDao.getTuiInfo(keyWord);
        if(tuiList.size()>1){
            setAttr("message","much");
            renderJson();
            return;
        }else  if(tuiList.size()==0){
            setAttr("message","no");
            renderJson();
            return ;
        }else {
            setAttr("message",tuiList.get(0));
            renderJson();
            return;
        }

    }

    /**
     * 把一条信息作为推荐信息
     * 说明：1. 这是普通编辑文章时调用的接口；2. 推荐后需要审核后才能到推荐集中营去；
     * <p>
     * 调用接口：http://ipaderess/editorJfinalProject/page/addRecom
     */
    public void addRecom() {
        int pageId = getParaToInt("pageId");
        try {
            PageEdit.pageEditDao.addRecom(pageId);
            setAttr("result","ok");
            renderJson();
        } catch (Exception exception) {
            LOGGER.error("把一条信息作为推荐信息出错...", exception);
        }
    }

    /**
     * 把一条已推荐的信息放回推荐集中营
     * 调用接口：http://ipaderess/editorJfinalProject/page/addRecom/{pageId}
     */
    public void removeRecom() {
        int pageId = getParaToInt("pageId");
        PageEdit.pageEditDao.removeRecom(pageId);

        setAttr("result", "ok");
        renderJson();
    }


    /**
     * 转到一级分类页面
     * 调用接口：http://ipaderess/editorJfinalProject/page/gotoFirstPage/firstType-pageNumber
     */
    public void gotoFirstPage() {
        int firstType = getParaToInt(0); // 获取一级分类id
        int pageNumber = getParaToInt(1); // 获取分页数

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        /**
         * 如果请求的是非推荐的一级页面
         * 发送信息有：id,pageDate,imageSrc,firstTitle,thirdTypeName,pageFrom,recomPos,isReady,name
         */
        Page<Record> pageFirst = PageFirstType.pageDao.getFirstPage(pageNumber, Properties.PageSize_Main, firstType);
        pageList = pageFirst.getList();

//        pageList.addAll(pageRecomList);

        if (pageFirst.getList().size() == 0) {
            LOGGER.error("查询一级分类信息出错，该一级分类未查询到信息");
        }

            /*获取总页数*/
        totalPage = PageFirstType.pageDao.getPageNumberFirst(Properties.PageSize_Main, firstType);

        /*分页信息发送到页面*/
        setAttr("firstType", firstType);
        setAttr("pageInfoList", pageList);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数

        /*获取明天的日期*/
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        Date dt = new Date();
        Calendar calendar = new GregorianCalendar();
        calendar.setTime(dt);
        calendar.add(calendar.DATE, 1);//把日期往后增加 1 天.整数往后推,负数往前移动
        dt=calendar.getTime(); //这个时间就是日期往后推 1 天的结果
        String dateString = simpleDateFormat.format(dt);
        setAttr("dateString",dateString);

        // 如果是转到推荐界面，则需要单独转到
        if (firstType == 0) {
            List<Record> pageRecomList = PageFirstType.pageDao.getRecomSlide();
//        System.out.println(pageRecomList);
//        for(Record record:pageRecomList){
//            setAttr("img1",record.get("imageSrc2"));
//        }
            setAttr("img1",pageRecomList.get(0).get("imageSrc2"));
            setAttr("img2",pageRecomList.get(1).get("imageSrc2"));
            setAttr("img3",pageRecomList.get(2).get("imageSrc2"));
            setAttr("img4",pageRecomList.get(3).get("imageSrc2"));
            setAttr("img5",pageRecomList.get(4).get("imageSrc2"));
            renderJsp("newRecom.jsp");
            return;
        }
        renderJsp("infoPage.jsp");
    }

    /**
     * 转到隐藏推荐窗口，支持删除
     */
    public void gotoHidePage() {
        int firstType = getParaToInt(0); // 获取一级分类id
        int pageNumber = getParaToInt(1); // 获取分页数

        /*定义将要发送给客户端的数据*/
        List<Record> pageList = null; //发送给网页端的信息数组
        int totalPage = 0; //请求信息总页数

        /**
         * 如果请求的是非推荐的一级页面
         * 发送信息有：id,pageDate,imageSrc,firstTitle,thirdTypeName,pageFrom,recomPos,isReady,name
         */
        Page<Record> pageFirst = PageFirstType.pageDao.getFirstPage(pageNumber, Properties.PageSize_Main, firstType);
        pageList = pageFirst.getList();

        if (pageFirst.getList().size() == 0) {
            LOGGER.error("查询一级分类信息出错，该一级分类未查询到信息");
        }

            /*获取总页数*/
        totalPage = PageFirstType.pageDao.getPageNumberFirst(Properties.PageSize_Main, firstType);

        /*分页信息发送到页面*/
        setAttr("firstType", firstType);
        setAttr("pageInfoList", pageList);
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        // 如果是转到推荐界面，则需要单独转到
        renderJsp("recom.jsp");

    }

    /**
     * 转到隐藏推荐划图列表，支持删除
     */
    public void gotoHideSlide(){
        int firstType = getParaToInt(0);
        List<Record> pageList = PageFirstType.pageDao.getSlidePage(firstType);

        if (pageList.size() == 0) {
            LOGGER.error("查询失败，未找到所需的划图文章！");
        }

        setAttr("pageInfoList", pageList);
        renderJsp("recom.jsp");

    }

    /**
     * 转到二级分类页面
     * 调用接口：http://ipaderess/editorJfinalProject/page/gotoSecondPage/secondType-pageNumber
     */
    public void gotoSecondPage() {
        int secondType = getParaToInt(0); // 获取二级分类id
        int pageNumber = getParaToInt(1); // 获取分页数

        /**
         * 查询分页信息
         * 发送的信息有：id,pageDate,editorId,imageSrc,firstTitle,thirdTypeName,pageFrom,isReady,sourceAddress
         */
        Page<Record> pageSecond = PageSecondType.pageDao.getSecondPage(pageNumber, Properties.PageSize_Main, secondType);

        if (pageSecond.getList().size() == 0) {
            LOGGER.error("查询一级分类信息出错，该一级分类未查询到信息");
        }

        /*获取总页数*/
        int totalPage = PageSecondType.pageDao.getPageNumberSecond(Properties.PageSize_Main, secondType);

        /*分页信息发送到页面*/
        setAttr("secondType", secondType);
        setAttr("pageInfoList", pageSecond.getList());
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("secondInfoPage.jsp");
    }

    /**
     * 转到三级分类页面
     * 调用接口：http://http:localhost:8080/editorJfinalProject/page/gotoThirdPage/thirdType-pageNumber
     */
    public void gotoThirdPage() {
        int thirdType = getParaToInt(0);
        int pageNumber = getParaToInt(1);

        /**
         * 查询分页信息
         * 发送信息有：id,pageDate,imageSrc,editorId,firstTitle,pageFrom,sourceAddress,isReady
         */
        Page<Record> pageThird = PageEdit.pageEditDao.getThirdPage(pageNumber, Properties.PageSize_Main, thirdType);

        if (pageThird.getList().size() == 0) {
            LOGGER.error("查询三级分类信息出错，该三级分类未查询到信息");
        }

        /*获取总页数*/
        int totalPage = PageEdit.pageEditDao.getPageNumberThird(Properties.PageSize_Main, thirdType);

        /*分页信息发送到页面*/
        setAttr("thirdType", thirdType);
        setAttr("pageInfoList", pageThird.getList());
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        /*这里发送到的页面应该是在page文件夹之下*/
        renderJsp("thirdInfoPage.jsp");
    }

    /**
     * 进入我的编辑界面
     * 访问地址：http://ipaderess/editorJfinalProject/page/editorPage/{editorId}-{pageNumber}
     */
    public void editorPage() {
        String editorId = getPara(0);
        int pageNumber = getParaToInt(1);

        /**
         * 查询分页信息
         * 发送的信息有：id,pageDate,thirdTypeName,imageSrc,firstTitle,pageFrom,sourceAddress,isReady
         */
        Page<Record> editorPage = PageEdit.pageEditDao.getEditorPage(editorId, pageNumber, Properties.PageSize_Main);

        if (editorPage.getList().size() == 0) {
            LOGGER.error("获得编辑人员个人编辑界面信息错误，没有查找到任何数据...");
        }

        // 获取总页数
        int totalPage = PageEdit.pageEditDao.getEditorPageNumber(editorId, Properties.PageSize_Main);

        // 分页信息发送到页面
        setAttr("pageInfoList", editorPage.getList());
        setAttr("currentPage", pageNumber); // 当前页
        setAttr("totalPage", totalPage); //总页数
        // 这里发送到的页面应该是在page文件夹之下
        renderJsp("editorPage.jsp"); // 我的编辑页面：editorPage.jsp
    }

    /**
     * 转到特定信息页面
     * 调用接口 http://ipaderess/editorJfinalProject/page/gotoPage/pageId
     */
    public void gotoPage() throws IOException {
        int pageId = getParaToInt(0);

        List<Record> recordList = PageEdit.pageEditDao.getPageInfo(pageId);

        /**
         * 如果数据去除错误
         */
        if(recordList.isEmpty()){
            LOGGER.error("根据pageId无法取得文章内容信息,pageId: "+pageId);
            renderHtml("");
            return;
        }

        String firstTitle = recordList.get(0).get("firstTitle");
        int readNumber = 0;
        Date pageDate = recordList.get(0).get("pageDate");
        String pageFrom = recordList.get(0).get("pageFrom");
        String imageSrc = recordList.get(0).get("pageImageSrc");
        String sourceAddress = recordList.get(0).get("sourceAddress");
        String pageContent = recordList.get(0).get("pageContent");
        String pageID = Integer.toString(pageId);
        String htmlString = "";

        String pageModelStr = GetEditPageModel.getEditPageModelStr(Properties.Page_ModelURL);
        htmlString = pageModelStr;
        htmlString = htmlString.replace("{kuibu_pageTitle}",firstTitle);
        htmlString = htmlString.replace("{kuibu_pageReadNumber}",String.valueOf(readNumber));
        htmlString = htmlString.replace("{kuibu_pageFrom}",pageFrom);
        htmlString = htmlString.replace("{kuibu_pageContent}",pageContent);
        htmlString = htmlString.replace("{kuibu_pageTitleImage}",imageSrc);
        htmlString = htmlString.replace("{kuibu_pageDate}",new SimpleDateFormat("yyyy-MM-dd").format(pageDate));
        htmlString = htmlString.replace("{kuibu_sourceAddress}",sourceAddress);
        htmlString = htmlString.replace("{kuibu_pageId}",pageID);

        renderHtml(htmlString);
    }

    /**
     * 删除特定信息
     * 调用接口：http://ipaderess/editorJfinalProject/page/delPage
     */
    public void delPage() {
        int pageId = getParaToInt("pageId");
        try {
            PageEdit.pageEditDao.deleteById(pageId);
            setAttr("result","ok");
            renderJson();
        } catch (Exception exception) {
            LOGGER.error("删除特定信息错误...", exception);
        }
    }

    /**
     * 把信息标识为审核通过
     * 调用接口：http://ipaderess/editorJfinalProject/page/pass/{id}
     */
    public void pass(){
        int pageId = getParaToInt("pageId");
        try {
            PageEdit.pageEditDao.pass(pageId);
            setAttr("result","ok");
            renderJson();
        } catch (Exception exception) {
            LOGGER.error("把信息标识为审核通过错误...", exception);
        }
    }

    /**
     * 审核通过的信息后悔了。。。，作死啊
     * 调用接口：http://ipaderess/editorJfinalProject/page/removePass/{id}
     */
    public void removePass(){
        int pageId = getParaToInt("pageId");
        try {
            PageEdit.pageEditDao.removePass(pageId);
            setAttr("result","ok");
            renderJson();
        } catch (Exception exception) {
            LOGGER.error("审核通过的信息改成审核未通过错误...", exception);
        }
    }

    /**
     * 设置主题文章的顺序
     */
    public void changeSubOrder(){
        int pageId = getParaToInt("pageId");
        int orderIndex = getParaToInt("orderIndex");
        try{
            PageEdit.pageEditDao.setSubOrder(pageId, orderIndex);
            setAttr("result","ok");
            renderJson();
        }catch (Exception exception) {
            LOGGER.error("修改主题文章的顺序出错啦啦啦。。。。",exception);
        }
    }

    /**
     * 更改精选文章的顺序
     */
    public  void changeJingOrder(){
        int pageId = getParaToInt("pageId");
        int orderIndex = getParaToInt("orderIndex");
        orderIndex = orderIndex + 2;
        try{
            PageEdit.pageEditDao.setJingOrder(pageId, orderIndex);
        }catch (Exception exception){
            LOGGER.error("修改精选文章的顺序出错啦啦啦。。。。",exception);
        }
    }

    /**
     * 更改整合文章的顺序
     */
    public void changeZhengOrder(){
        int pageId = getParaToInt("pageId");
        int orderIndex = getParaToInt("orderIndex");
        orderIndex = orderIndex + 6;
        try{
            PageEdit.pageEditDao.setJingOrder(pageId, orderIndex);
        }catch (Exception exception){
            LOGGER.error("修改整合文章的顺序出错啦啦啦。。。。",exception);
        }
    }

    /**
     * 转到推荐界面
     * 调用接口:http://ipaderess/editorJfinalProject/page/getRecomPageInfo
     */
    public void getRecomPageInfo(){

    }

    /**
     * 这是个危险接口，大兄弟测试专用！！！
     */
    public void fortest(){
        String name = getPara("name");
//        String email = getPara("email");
//        String message = getPara("message");
        System.out.println(name);
        String temp = "[{\"c01\":\"1\",\"c02\":\"2\",\"c03\":\"3\",\"c04\":\"4\",\"c05\":\"5\",\"c06\":\"6\",\"c07\":\"7\",\"c08\":\"8\",\"c09\":\"9\"},{\"c01\":\"2\",\"c02\":\"4\",\"c03\":\"5\",\"c04\":\"2\",\"c05\":\"8\",\"c06\":\"11\",\"c07\":\"21\",\"c08\":\"1\",\"c09\":\"12\"},{\"c01\":\"5\",\"c02\":\"1\",\"c03\":\"4\",\"c04\":\"11\",\"c05\":\"9\",\"c06\":\"8\",\"c07\":\"1\",\"c08\":\"8\",\"c09\":\"2\"}]";
        //[{"c01":"1","c02":"2","c03":"3","c04":"4","c05":"5","c06":"6","c07":"7","c08":"8","c09":"9"},{"c01":"2","c02":"4","c03":"5","c04":"2","c05":"8","c06":"11","c07":"21","c08":"1","c09":"12"},{"c01":"5","c02":"1","c03":"4","c04":"11","c05":"9","c06":"8","c07":"1","c08":"8","c09":"2"}]
        setAttr("ss",temp);
        renderJson();
    }
}
